package com.iotechn.unimall.admin.api.leaseSeed;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.toolkit.StringUtils;
import com.iotechn.unimall.core.exception.AdminServiceException;
import com.iotechn.unimall.core.exception.ExceptionDefinition;
import com.iotechn.unimall.core.exception.ServiceException;
import com.iotechn.unimall.data.domain.LeaseSeedDO;
import com.iotechn.unimall.data.mapper.LeaseSeedMapper;
import com.iotechn.unimall.data.model.Page;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


@Service
public class AdminLeaseSeedServiceImpl implements AdminLeaseSeedService {

    @Autowired
    private LeaseSeedMapper leaseSeedMapper;

    @Override
    public Page<LeaseSeedDO> list(Integer page, Integer limit,Long adminId) throws ServiceException {
        Wrapper<LeaseSeedDO> wrapper = new EntityWrapper<LeaseSeedDO>();
        wrapper.orderBy("id", false);
        Integer count = leaseSeedMapper.selectCount(wrapper);
        List<LeaseSeedDO> leaseSeedDOS = leaseSeedMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
        return new Page<>(leaseSeedDOS, page, limit, count);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public LeaseSeedDO create(LeaseSeedDO leaseSeedDO, Long adminId) throws ServiceException {
        if (leaseSeedMapper.insert(leaseSeedDO) > 0) {
            leaseSeedDO.setId(leaseSeedDO.getId());
            return leaseSeedDO;
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String update(LeaseSeedDO leaseSeedDO, Long adminId) throws ServiceException {
        Long id = leaseSeedDO.getId();
        if (id == null) {
            return "fail";
        }
        if (leaseSeedMapper.updateById(leaseSeedDO) > 0) {
            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String delete(Long id, Long adminId) throws ServiceException {
        if (leaseSeedMapper.deleteById(id) > 0) {
            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);

    }

    @Override
    public LeaseSeedDO detail(Long id, Long adminId) throws ServiceException {
        return leaseSeedMapper.selectById(id);
    }


}
